from google.appengine.ext import db, blobstore
from google.appengine.api import users

class User(db.Model):
	"""Represents a user's rating of a seller or item and the
	associated comments"""
	id = db.UserProperty(required=True, auto_current_user_add=True)
	email = db.EmailProperty(required=True)

	first_name = db.StringProperty()
	last_name = db.StringProperty()
	address = db.PostalAddressProperty()
	phone_number = db.PhoneNumberProperty()

	is_admin = db.BooleanProperty()
	registered = db.DateTimeProperty(auto_now_add=True)
	last_loggedin = db.DateTimeProperty(auto_now_add=True)
	#last_IP = db.IntegerProperty()

	info = db.TextProperty()
	photo = blobstore.BlobReferenceProperty()
	video = blobstore.BlobReferenceProperty()

	@classmethod
	def getUser(cls, user):
		if not user:
			return None

		q = cls.all()
		q.filter('id =', user)

		obj = q.get()
		if obj is None:
			obj = cls(id=user, email=user.email())
			obj.put()
		return obj

	def getName(self):
		if self.first_name and self.last_name:
			return self.first_name + ' ' + self.last_name
		elif self.first_name or self.last_name:
			return self.first_name if self.first_name else self.last_name
		else:
			return "Unnamed User"

	def getPhoto(self):
		return blobstore.BlobInfo.get(self.photo)

	def getVideo(self):
		return blobstore.BlobInfo.get(self.video)
